package com.jinyang.jetpackdemo.util;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

/**
 * @Author: LiuJinYang
 * @CreateDate: 2021/8/11
 * @Description:
 */
public class LocationUtil {
    private CallBack callBack;

    private LocationUtil() {
    }

    public static LocationUtil getInstance() {
        return LocationUtilHolder.mInstance;
    }

    private static class LocationUtilHolder {
        private static final LocationUtil mInstance = new LocationUtil();
    }


    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public void removeCallBack() {
        this.callBack = null;
    }

    /**
     * 获取定位方法
     */
    public void getLocation(Context context) {
        //1.获取系统LocationManager服务
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        //低精度，中精度高精度获取不到location。
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        //海拔
        criteria.setAltitudeRequired(true);
        //方位
        criteria.setBearingRequired(true);
        //允许有花费
        criteria.setCostAllowed(true);
        //低功耗
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String locationProvider = locationManager.getBestProvider(criteria, true);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            LjyLogUtil.d("没有权限 ");
            if (callBack != null) {
                callBack.onCall("没有权限");
            }
            return;
        }
        //2.获取GPS最近的定位信息
        Location location = locationManager.getLastKnownLocation(locationProvider);

        //3.将location里面的位置信息展示在edittext中
        updateLocation(location);
        //4.设置没两秒获取一次GPS的定位信息
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                //当GPS定位信息发生改变时，更新位置
                updateLocation(location);
            }

            @Override
            public void onProviderEnabled(@NonNull String provider) {
                //当GPS LocationProvider可用时，更新位置
                if (callBack != null) {
                    callBack.onCall("onProviderEnabled:" + provider);
                }
            }

            @Override
            public void onProviderDisabled(@NonNull String provider) {
                if (callBack != null) {
                    callBack.onCall("onProviderDisabled:" + provider);
                }
            }
        });
    }

    private void updateLocation(Location location) {
        if (location != null) {
            String sb = "实时的位置信息：\n经度：" +
                    location.getLongitude() +
                    "\n纬度：" +
                    location.getLatitude() +
                    "\n高度：" +
                    location.getAltitude() +
                    "\n速度：" +
                    location.getSpeed() +
                    "\n方向：" +
                    location.getBearing() +
                    "\n精度：" +
                    location.getAccuracy();
            if (callBack != null) {
                callBack.onCall(sb);
            }
        } else {
            if (callBack != null) {
                callBack.onCall("location == null");
            }
        }

    }


    public interface CallBack {
        public void onCall(String info);
    }
}
